package org.example.spring.self.demo.pb;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 文件描述
 *
 * @author yezhou41127
 * @date: 2022年09月01日
 */
@Component
// Spring can autowire a single prototype instance when it creates the singleton. It’s the default framework behavior.
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE,
		// Spring can create a new prototype instance on every call to any method of this prototype.
		// Injecting on prototype method call
		proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MessageBuilder {

	private static final AtomicInteger adder = new AtomicInteger(0);

	MessageBuilder() {
		adder.incrementAndGet();
	}

	static int getAdderCounter() {
		return adder.get();
	}

	private String content;
	private String receiver;

	MessageBuilder withContent(String content) {
		this.content = content;
		return this;
	}

	MessageBuilder withReceiver(String receiver) {
		this.receiver = receiver;
		return this;
	}

	Message build() {
		return new Message(content, receiver);
	}
}
